package cn.seecoder.courselearning.serviceimpl.course;

import cn.seecoder.courselearning.mapperservice.course.RentCourseMapper;
import cn.seecoder.courselearning.service.course.RentService;
import cn.seecoder.courselearning.vo.course.RentCourseVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wangzb
 * @date 2021/7/8
 * @description
 */
@Service
public class RentCourseServiceImpl implements RentService {
    @Resource
    private RentCourseMapper rentCourseMapper;
    /**
     * 查询rent列表
     *
     * @param userId 用户id
     * @return List
     */
    @Override
    public List<RentCourseVO> qryRentList(Integer userId) {
        List<RentCourseVO> rentList= rentCourseMapper.qryRentList(userId);
        Date date = new Date();
        if (rentList.stream().allMatch(s->s.getRentId()==null)){
            return new ArrayList<>();
        }
        //到期刷新不跑定时任务，查询时来处理
        rentList.stream().filter(s->date.after(s.getEndTime())).map(RentCourseVO::getRentId).forEach(s-> rentCourseMapper.remove(s));
        return rentList.stream().filter(s->date.before(s.getEndTime())&&date.after(s.getStartTime())).collect(Collectors.toList());
    }
}
